<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**产品类型关联表model */
class ResourceSystemTypeIntention extends BaseModel
{

    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    public $table = 'resource_system_type_intention';

    /**
     * 获取资源类型资源数据列表
     * @param limit int 分页大小
     * @param keywords int 检索id
     * @param big_type_id 系统大类id
     * @param resource_system_type_id 书单id
     */
    public function lists($search_data = [])
    {
        $resource_system_type_id = $search_data['resource_system_type_id'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        $keywords = !empty($search_data['keywords']) ? $search_data['keywords'] : null;
        $big_type_id = !empty($search_data['big_type_id']) ? $search_data['big_type_id'] : null;
        //获取敏感词
        // $sensitiveWordModel = new SensitiveWord();
        // $sensitive_word = $sensitiveWordModel->getSensitiveWordALL();
        //获取书单数据
        $field = ['i.id', 'i.resource_id', 'i.create_time', 'r.guid', 'r.title', 'r.author', 'r.isbn', 'r.press', 'r.pre_time', 'r.img', 'r.intro'];
        $res = $this->from($this->getTable() . " as i")->select($field)
            ->join('resource_total as r', 'r.id', '=', 'i.resource_id')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('r.title', 'like', "%$keywords%");
                }
            })
            ->where(function ($query) use ($big_type_id) {
                //系统大类
                if (!empty($big_type_id)) {
                    $big_type_id = !is_array($big_type_id) ? explode(',', $big_type_id) : $big_type_id;
                    $query->whereIn('r.big_type_id', $big_type_id);
                }
            })
            ->where('i.type_id', $resource_system_type_id)
            ->where('is_mask', 1)
            ->where('r.is_del', 1)
            ->orderByDesc('i.id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     * 根据资源类型获取资源大类id
     * @param resource_system_type_id 资源类型id (小类)
     */
    public function getBigTypeIdBySystemTypeId($resource_system_type_id)
    {
        $res = $this->from($this->getTable() . ' as i')
            ->join('resource_total as r', 'r.id', '=', 'i.resource_id')
            ->where('r.is_del', 1)
           // ->where('i.is_del', 1)
            ->where(function ($query) use ($resource_system_type_id) {
                if ($resource_system_type_id) {
                    $resource_system_type_id = !is_array($resource_system_type_id) ? explode(',', $resource_system_type_id) : $resource_system_type_id;
                    $query->whereIn('i.type_id', $resource_system_type_id);
                }
            })
            ->groupBy('big_type_id')
            ->pluck('big_type_id')
            ->toArray();

        return $res;
    }



    /**
     * 添加资源类型作品
     * @param resource_system_type_id int 书单id
     * @param resource_ids int 资源作品的id 多个逗号拼接
     *
     *
     *template 模版 1~7 只能为书  模版8只能为视频，模版9可以书和视频混排
     */
    public function addResourceSystemTypeResource($resource_system_type_id, $resource_ids)
    {
        $resource_system_type_info = ResourceSystemType::select('way', 'template')->where('id', $resource_system_type_id)->first();
        if (in_array($resource_system_type_info['way'], [2, 3, 4])) {
            throw new Exception('该类型不能添加作品');
        }

        $old_resource_ids = $this->where('type_id', $resource_system_type_id)->pluck('resource_id')->toArray();
        $resource_ids = explode(',', $resource_ids);
        $resourceTotalModel = new ResourceTotal();
        $data = [];
        $i = 0;
        foreach ($resource_ids as $key => $val) {
            $resource_total_info = $resourceTotalModel->select('resource_business_id', 'big_type_id')->where('id', $val)->where('is_del', 1)->first();
            if (in_array($resource_system_type_info['template'], [0, 1, 2, 3, 4, 5, 6, 7]) && $resource_total_info['big_type_id'] == 3) {
                throw new Exception('该模版不能添加视频资源');
            }
            if (in_array($resource_system_type_info['template'], [8]) && $resource_total_info['big_type_id'] != 3) {
                throw new Exception('该模版只能添加视频资源');
            }
            if (!in_array($val, $old_resource_ids)) {
                $data[$i]['type_id'] = $resource_system_type_id;
                $data[$i]['resource_id'] = $val;
                $data[$i]['resource_business_id'] = $resource_total_info['resource_business_id'];
                $data[$i]['create_time'] = date('Y-m-d H:i:s');
                $data[$i]['change_time'] = date('Y-m-d H:i:s');
                $i++;
            }
        }
        if ($data) {
            $this->insert($data);
        }
        return true;
    }


    /**
     * 删除资源作品
     * @param resource_system_type_id int 书单id
     * @param resource_list_ids  数据id 多个逗号拼接
     */
    public function delResourceSystemTypeResource($resource_system_type_id, $resource_list_ids)
    {
        $resource_system_type_way = ResourceSystemType::where('id', $resource_system_type_id)->value('way');
        if (in_array($resource_system_type_way, [2, 3, 4])) {
            return false; //不允许删除
        }

        $resource_list_ids = explode(',', $resource_list_ids);
        return $this->where('type_id', $resource_system_type_id)->whereIn('id', $resource_list_ids)->delete();
    }

    /**
     * 获取系统类型作品id
     * @param resource_system_type_id int 书单id
     */
    public function getResourceSystemTypeIntentionResourceId($resource_system_type_id)
    {
        $resource_system_type_way = ResourceSystemType::where('id', $resource_system_type_id)->value('way');
        if (in_array($resource_system_type_way, [2, 3, 4])) {
            return []; //无数据
        }
        return $this->where('type_id', $resource_system_type_id)->pluck('resource_id')->toArray();
    }
}
